package grails.company

class DepartmentController {

    def list() {
        def departments = Department.list()
        if (departments) {
            render(contentType: 'text/json') {
                success = true
                data = Department.list().collect {
                    [
                            'id': it.id,
                            'title': it.title,
                            'description': it.description,
                            'parentId': it?.organization?.id
                    ]
                }
            }
        } else {
            render(contentType: 'text/json') { success = false }
        }
    }

    def view() {
        if (!params?.data?.id) {
            list()
            return
        }

        def dep = Department.findById(params.data.id)
        if (dep) {
            render(contentType: 'text/json') {
                success = true
                data = {
                    id = dep.id
                    title = dep.title
                    description = dep.description
                    parentId = dep.organization.id
                }
            }
        } else {
            render(contentType: 'text/json') { success = false }
        }
    }

    def update() {
        if (!params?.data?.id || !params?.data?.title) {
            render(contentType: 'text/json') { success = false }
            return
        }

        def dep = Department.findById(params.data.id)
        if (!dep) {
            render(contentType: 'text/json') { success = false }
            return
        }

        dep.properties['title', 'description'] = params.data
        if (!dep.validate()) {
            render(contentType: 'text/json') { success = false }
            return
        }

        dep.save()
        render(contentType: 'text/json') { success = true }
    }

    def delete() {
        if (!params?.data?.id) {
            render(contentType: 'text/json') { success = false }
            return
        }

        def dep = Department.findById(params.data.id);
        if (dep) {
            dep.delete()
            render(contentType: 'text/json') { success = true }
        } else {
            render(contentType: 'text/json') { success = false }
        }
    }

    def create() {
        if (!params?.data?.parentId) {
            render(contentType: 'text/json') { success = false }
            return
        }

        def organization = Organization.findById(params.data.parentId)
        if (!organization) {
            render(contentType: 'text/json') { success = false }
            return
        }

        def department = new Department();
        department.properties['title', 'description'] = params.data

        department.organization = organization

        if (!department.validate()) {
            render(contentType: 'text/json') { success = false }
            return
        }


        if (department.save()) {
            organization.addToDepartments(department)
            render(contentType: 'text/json') {
                success = true
                data = {
                    id = department.id
                    title = department.title
                    description = department.description
                    parentId = department.organization.id
                }
            }
        } else {
            render(contentType: 'text/json') { success = false }
        }
    }

}
